/* *
 * 私家之旅
 * ©2014 - 12 - 25
 * */
 

;(function($) {
    $.fn.extend({
		//图片放大缩小 {mode:"in",scale:0.5} in:缩小 out:放大 scale:缩放比例，默认0.05
		zoom:function(param){
			this.each(function(){
				var $img = $(this);
				param = $.extend({mode:"out",scale:0.05},param);
				var mode = param.mode ? param.mode : out;
				var scale = param.scale ? param.scale : 0.05;
				var $wrap = $img.parent();
				var width = $img.width();
				var height = $img.height();
				
				$wrap.css({
					"position":"relative",
					"display":"block",
					"overflow":"hidden",
					"width":width,
					"height":height
				});
				$img.css({"position":"absolute"});
				
				if(mode == "out"){
					$wrap.hover(function(){
						$img.stop(true).animate({
							"left":- width * scale,
							"top":- height * scale,
							"width":width + width * scale * 2,
							"height":height + height * scale * 2
						});
					},function(){
						$img.stop(true).animate({
							"left":0,
							"top":0,
							"width":width,
							"height":height
						});
					});
				}
				if(mode == "in"){
					$img.css({
						"left":- width * scale,
						"top":- height * scale,
						"width":width + width * scale * 2,
						"height":height + height * scale * 2
					});
					$wrap.hover(function(){
						$img.stop(true).animate({
							"left":0,
							"top":0,
							"width":width,
							"height":height
						});
					},function(){
						$img.stop(true).animate({
							"left":- width * scale,
							"top":- height * scale,
							"width":width + width * scale * 2,
							"height":height + height * scale * 2
						});
					});
				}
			});
		},
		//下拉列表框
		selected:function(){
			this.each(function(){
				var $wrap = $(this);
				var $info = $wrap.find(".info");
				var $item = $wrap.find(".item");
				var isInput = $info;
				if(isInput.length != 0){
					isInput = isInput.get(0).tagName.toLowerCase() == "input" ? true : false;
				}else{
					isInput = false;
				}
				
				$wrap.click(function(){
					$item.toggle();
					if($item.is(":visible")){
						$wrap.css({"z-index":2});
					}else{
						$wrap.css({"z-index":1});
					}
				});
				
				$item.find("li").each(function(index, element) {
                    $(this).click(function(){
						if(isInput){
							$info.val($(this).text());
						}else{
							$info.text($(this).text());
						}
						$info.attr("data-val",$(this).attr("data-val"));
					});
                });
				
				$(document).on("click",function(e){
					if($(e.target).parents(".select").length == 0){
						$item.hide();
					}
				});
			});
		},
		//侧边栏
		sideBar:function(){
			this.each(function(){
				var $wrap = $(this);
				var $btn = $wrap.find(".gotop");
				var windowWidth = $(window).width();
				var wrapWidth = $wrap.width();
				var top = ($(window).height() - $wrap.height()) / 2;
				var height =isNaN(parseInt($(".silder").attr("data-height"),10)) ? $(".head").height() : $(".head").height() + parseInt($(".silder").attr("data-height"),10);
				height = height + 100;
				
				if(height <= 800){
					$wrap.css({"top":height});
				}else{
					$wrap.css({"position":"fixed","top":top});
					$btn.show();
					$btn.click(function(){
						$("html,body").animate({"scrollTop":0});
					});
					return false;
				}
				function fixedPosition(){
					var right = parseInt(((windowWidth - 1200) / 4) - (wrapWidth / 2),10);
					right = right > 0 ? right : 0;
					$wrap.css({"right":right});
				}
				
				fixedPosition();
				$(window).on("resize",function(){
					windowWidth = $(window).width();
					fixedPosition();
				});
				
				$(window).scroll(function(){
					var _top = $(window).scrollTop();
					
					if(_top >= height - top){
						$btn.show();
						$wrap.css({"position":"fixed","top":top});
					}else{
						$btn.hide();
						$wrap.css({"position":"absolute","top":height});
					}
				});
				$btn.click(function(){
					$("html,body").animate({"scrollTop":0});
				});
			});
		},
		//placeholder
        placeholder: function() {
            if ('placeholder' in document.createElement('input')) {
                return this
            } else {
                return this.each(function() {
                    var _this = $(this),
                    this_placeholder = _this.attr('placeholder');
                    _this.val(this_placeholder).focus(function() {
                        if (_this.val() === this_placeholder) {
                            _this.val('')
                        }
                    }).blur(function() {
                        if (_this.val().length === 0) {
                            _this.val(this_placeholder)
                        }
                    })
                })
            }
        },
		//幻灯
		silder:function(){
			this.each(function(){
				var $wrap = $(this);
				var $item = $wrap.find(".item");
				var auto = $wrap.attr("data-switch") ? $wrap.attr("data-switch") : "off";
				var speed = isNaN(parseInt($wrap.attr("data-speed"),10)) ? false : parseInt($wrap.attr("data-speed"),10);
				var height = $wrap.attr("data-height");
				var size = $item.size();
				var index = 0;
				var speed = 4000;
				
				$wrap.css({"height":height,"z-index":1});
				
				$item.each(function(index, element) {
                    $(this).css({
						"position":"absolute",
						"left":0,
						"top":0,
						"width":"100%",
						"height":"100%",
						"z-index":1,
						"opacity":1,
						"background":"url(" + $(this).attr("data-url") + ") no-repeat center top"
					});
                });
				$item.eq(0).css({"z-index":2});
				
				var $prev = $('<a href="javascript:void(0);" class="prev"></a>').appendTo($wrap);
				var $next = $('<a href="javascript:void(0);" class="next"></a>').appendTo($wrap);
				var $btnw = $('<div class="btn"></div>').appendTo($wrap);
				
				var tmp = '<span class="cur"></span>';
				for(i = 1; i < size; i++){
					tmp += '<span></span>';
				}
				$(tmp).appendTo($btnw);
				var $btn = $btnw.children();
				
				if(auto == "off"){
					$prev.css({"display":"none"});
					$next.css({"display":"none"});
					$btnw.css({"display":"none"});
				}
				
				$prev.click(function(){
					index--;
					if(index < 0)
						index = size - 1;
					
					animate(index);
				});
				
				$next.click(function(){
					index++;
					if(index > size - 1)
						index = 0;
					
					animate(index);
				});
				
				$btn.each(function(index){
					$(this).mouseover(function(){
						animate(index);
					});
				})
				
				if(!!speed && auto == "on"){
					timer = setInterval(function(){$next.trigger("click");}, speed);
					$wrap.mouseenter(function(){
						clearInterval(timer);
					}).mouseleave(function(){
						timer = setInterval(function(){$next.trigger("click");}, speed);
					})
				}
				
				function animate(index){
					$item.css({"z-index":1}).eq(index).css({"z-index":2,"opacity":0}).stop(true,true).animate({"opacity":1},"slow");
					$btn.removeClass("cur").eq(index).addClass("cur");
				}
			});
		}
    })
})(jQuery);

//按钮选中
var btnFocus = function(options){
	var opts = [];
	this.init = function(){
		opts = {
			btn: options.btn,
			current: options.current,
			multiple:options.multiple || false
		}
		opts.btn.click(function(){
			if(opts.multiple){
				$(this).toggleClass(opts.current);	
			}else{
				opts.btn.removeClass(opts.current);
				$(this).addClass(opts.current);
			}
		})
	}
	this.init();
}

//选项卡切换
var tabChange = function(options){
	var self = this,opts = [];
	this.init = function(){
		opts = {
			box: options.box,
			tab: options.tab,
			con: options.con,
			current: options.current,
			mouseEvent: options.mouseEvent == undefined ? "click" : options.mouseEvent.toLowerCase()
		}
		opts.tab.on(opts.mouseEvent,function(){
			var that = this;
			function extendFun(){
				self.changeCon(that)
			}
			if(opts.mouseEvent!=="click"){
				var timeDelay = setTimeout(extendFun,200);
				$(that).mouseleave(function(){clearTimeout(timeDelay)})	
			}else{
				extendFun()
			}
		})
	}
	this.changeCon = function(obj){
		var index = opts.tab.index($(obj));
		opts.tab.removeClass(opts.current)
		$(obj).addClass(opts.current);
		opts.con.hide().eq(index).show();
	}
	this.init();
}

$(document).ready(function(e) {
	$(":input[placeholder]").placeholder();
	$(".silder").silder();
	$(".side_bar").sideBar();
	$(".select").selected();
});
